package com.jwts.secret;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

/**
 * 采用MD5加密
 * 
 * @author 靳兴旺
 * @version 1.0.0 2020-7-23
 */
public class Md5Utils {

    /**
     * MD5加密 生成32位md5码
     * @param inStr 待加密字符串
     * @return 返回32位md5码
     */
    public static String md5Encode(String inStr) {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] byteArray = inStr.getBytes(StandardCharsets.UTF_8);
            byte[] md5Bytes = md5.digest(byteArray);
            StringBuilder md5Value = new StringBuilder();
            for (byte md5Byte : md5Bytes) {
                int val = ((int) md5Byte) & 0xff;
                if (val < 16) {
                    md5Value.append("0");
                }
                md5Value.append(Integer.toHexString(val));
            }
            return md5Value.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

}